﻿using Dapper;
using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.SysManage;
using LJX.SmartPark.Service.Repository;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.SysManage
{
    public class DepartManageServeice : IDepartManageServeice
    {
        private readonly IRepository<DepartManage> _departManageService;

        public DepartManageServeice(IRepository<DepartManage> departManageService)
        {
            _departManageService = departManageService;
        }

        private const string connString = "server=10.31.64.28;uid=sa;pwd=123456;database=SmartParkB";

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeleteDepartManageAsync(DepartManage input)
        {
            var result = await _departManageService.DeleteByIdAsync(input.MapTo<DepartManage>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <returns></returns>
        public Response <List<DepartManage>> GetDepartManageAsync()
        {
            var result = new List<DepartManage>();
            using(DbConnection conn=new SqlConnection(connString))
            {
                conn.Open();
                //Dapper 操作存储过程
                result =conn.Query<DepartManage>("DepartManage11",CommandType.StoredProcedure).ToList<DepartManage>();
                return Response<List<DepartManage>>.Ok(result);
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<DepartManage> GetDepartManageById(DepartManage input)
        {
            var departManage = _departManageService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = departManage.MapTo<DepartManage>();
            return Response<DepartManage>.Ok(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PostAddDepartManageListAsync(DepartManage input)
        {
            var result = _departManageService.InsertAsyncById(input.MapTo<DepartManage>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdDepartManageAsync(DepartManage input)
        {
            var result = _departManageService.UpdateAsync(input.MapTo<DepartManage>());

            return Response<bool>.Ok(result.Result > 0);
        }
    }
}
